/*
 * MainFrame.java
 *
 * Created on 13 maart 2008, 14:18
 */
package gui;

import Controller.ToolbarController;
import action.*;
import model.DiagramModel;
import diagram.*;
import java.awt.BorderLayout;
import model.ToolbarModel;
import serialize.Serializer;

/**
 *
 * @author  wijnand.schepens@hogent.be
 */
public class MainFrame extends javax.swing.JFrame {

    private DiagramPanel diagramPanel;
    private DiagramModel model;
    private ToolbarModel tmodel;
   // private ToolbarPopup popup;
 
    public MainFrame(DiagramModel model, ToolbarModel tmodel) {
        this.model = model;
        this.tmodel = tmodel;
        this.diagramPanel = new DiagramPanel(model);

        initComponents();
        pnl.add(diagramPanel, BorderLayout.CENTER);
        pack();
        Toolbar t = new Toolbar(tmodel);
        ToolbarController tc = new ToolbarController(tmodel, t);
       // t.setSize(20, 100);
        pnl.add(t, BorderLayout.EAST);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tlb = new javax.swing.JToolBar();
        btnAddNode = new javax.swing.JButton();
        btnAddHandle = new javax.swing.JButton();
        btnRef = new javax.swing.JButton();
        btnClrAll = new javax.swing.JButton();
        btnDelNode = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        lblDisplay = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        btnSerialize = new javax.swing.JButton();
        btnDeserialize = new javax.swing.JButton();
        pnl = new javax.swing.JPanel();
        mnu = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tlb.setRollover(true);

        btnAddNode.setAction(new AddAction("normal", null, model));
        btnAddNode.setText("V");
        btnAddNode.setFocusable(false);
        btnAddNode.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAddNode.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAddNode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNodeActionPerformed(evt);
            }
        });
        tlb.add(btnAddNode);

        btnAddHandle.setText("C");
        btnAddHandle.setEnabled(false);
        btnAddHandle.setFocusable(false);
        btnAddHandle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAddHandle.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAddHandle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddHandleActionPerformed(evt);
            }
        });
        tlb.add(btnAddHandle);

        btnRef.setText("   Refresh");
        btnRef.setFocusable(false);
        btnRef.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRef.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRef.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefActionPerformed(evt);
            }
        });
        tlb.add(btnRef);

        btnClrAll.setAction(new ClearAction(model));
        btnClrAll.setText("clearAll");
        btnClrAll.setFocusable(false);
        btnClrAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClrAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnClrAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClrAllActionPerformed(evt);
            }
        });
        tlb.add(btnClrAll);

        btnDelNode.setText("DeleteNode");
        btnDelNode.setFocusable(false);
        btnDelNode.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDelNode.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDelNode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelNodeActionPerformed(evt);
            }
        });
        tlb.add(btnDelNode);
        tlb.add(jSeparator1);

        lblDisplay.setText("   µ?   ");
        tlb.add(lblDisplay);

        jButton1.setText("savejpg");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        tlb.add(jButton1);

        btnSerialize.setText("Serialize");
        btnSerialize.setFocusable(false);
        btnSerialize.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSerialize.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSerialize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSerializeActionPerformed(evt);
            }
        });
        tlb.add(btnSerialize);

        btnDeserialize.setText("Deserialize");
        btnDeserialize.setFocusable(false);
        btnDeserialize.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDeserialize.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDeserialize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeserializeActionPerformed(evt);
            }
        });
        tlb.add(btnDeserialize);

        pnl.setLayout(new java.awt.BorderLayout());

        jMenu1.setText("File");
        mnu.add(jMenu1);

        jMenu2.setText("Edit");
        mnu.add(jMenu2);

        setJMenuBar(mnu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tlb, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
            .addComponent(pnl, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tlb, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnl, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddNodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNodeActionPerformed
        lblDisplay.setText("Node");
        btnAddHandle.setEnabled(true);
        getDiagramPanel().repaint();
}//GEN-LAST:event_btnAddNodeActionPerformed

    private void btnAddHandleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddHandleActionPerformed
        lblDisplay.setText("Trigger");
        btnAddHandle.setAction(new AddAction("triggerhandle", null, model));
        getDiagramPanel().repaint();
}//GEN-LAST:event_btnAddHandleActionPerformed

    private void btnRefActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefActionPerformed
        getDiagramPanel().repaint();
}//GEN-LAST:event_btnRefActionPerformed

    private void btnDelNodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelNodeActionPerformed
        getDiagramPanel().repaint();
    }//GEN-LAST:event_btnDelNodeActionPerformed

    private void btnClrAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClrAllActionPerformed
        getDiagramPanel().repaint();
    }//GEN-LAST:event_btnClrAllActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        getDiagramPanel().saveJPG();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnSerializeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSerializeActionPerformed
        Serializer s = new Serializer(model);
        s.serialize();// TODO add your handling code here:
}//GEN-LAST:event_btnSerializeActionPerformed

    private void btnDeserializeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeserializeActionPerformed
        Serializer s = new Serializer(model);
        s.deserialize();
    }//GEN-LAST:event_btnDeserializeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddHandle;
    private javax.swing.JButton btnAddNode;
    private javax.swing.JButton btnClrAll;
    private javax.swing.JButton btnDelNode;
    private javax.swing.JButton btnDeserialize;
    private javax.swing.JButton btnRef;
    private javax.swing.JButton btnSerialize;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JLabel lblDisplay;
    private javax.swing.JMenuBar mnu;
    private javax.swing.JPanel pnl;
    private javax.swing.JToolBar tlb;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the diagramPanel
     */
    public DiagramPanel getDiagramPanel() {
        return diagramPanel;
    }
}
